{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 定位元素的方式\n",
    "定位元素的原则：不管用什么方式定位，这个方式（`tag_name`、`name`、`id`等）必须全局唯一  \n",
    "\n",
    "1. `id`：如果存在，可以全局唯一定位一个元素  \n",
    "2. `name`：存在并且全局唯一才可以定位到  \n",
    "3. `class name`：存在并且全局唯一才可以定位到  \n",
    "4. `link text`：必须是链接，且链接内容全局唯一，才可以定位到  \n",
    "5. `partial link text`：必须是链接，且链接内容全局唯一，才可以定位到  \n",
    "6. `tag name标签`：必须全局唯一才可以定位到  \n",
    "7. `xpath`：任何一个元素都可以定位到  \n",
    "8. `css selector`：可通过元素的`id`、`class`、标签等属性组合定位，合理编写选择器可精准定位元素，需保证选择器对应元素全局唯一 ，能定位各类符合规则的元素 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By  # 需导入By类\n",
    "from selenium.webdriver.chrome.service import Service\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "import time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(1)  # 等待3秒\n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### class_name、xpath"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过class_name定位搜索框并输入内容\n",
    "driver.find_element(By.CLASS_NAME, \"sec-input\").send_keys(\"selenium\")\n",
    "time.sleep(1)  # 等待1秒\n",
    "\n",
    "# 通过xpath定位搜索按钮并点击（修正后）\n",
    "driver.find_element(By.XPATH, \"//*[@id='stb']\").click()\n",
    "\n",
    "\n",
    "time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### name、css_selector"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "\n",
    "driver.find_element(By.NAME, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(1)  # 等待1秒\n",
    "\n",
    "\n",
    "driver.find_element(By.CSS_SELECTOR, \"#stb\").click()\n",
    "\n",
    "\n",
    "time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### link text、partial link text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "\n",
    "\n",
    "driver.find_element(By.LINK_TEXT, \"视频\").click()\n",
    "\n",
    "\n",
    "time.sleep(3)  # 等待搜索结果加载\n",
    "\n",
    "driver.find_element(By.PARTIAL_LINK_TEXT, \"医\").click()\n",
    "\n",
    "time.sleep(3)  # 等待搜索结果加载\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### send_keys()、click()、submit()、clear()、text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "\n",
    "time.sleep(3)  \n",
    "\n",
    "driver.find_element(By.XPATH, \"//*[@id='query']\").clear()  # 清除搜索框内容\n",
    "time.sleep(2)  \n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"python\")\n",
    "time.sleep(1)  \n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(1)  \n",
    "\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网页\n",
      "微信\n",
      "知乎\n",
      "图片\n",
      "视频\n",
      "医疗\n",
      "汉语\n",
      "翻译\n",
      "问问\n",
      "百科\n",
      "地图\n",
      "更多\n"
     ]
    }
   ],
   "source": [
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "text = driver.find_element(By.XPATH, \"//*[@id='wrap']/div[1]/div[1]\").text\n",
    "time.sleep(1)\n",
    "print(text)  # 打印搜索框内容\n",
    "time.sleep(1)\n",
    "\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 固定等待、隐式等待"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#  固定等待\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"鹿晗\")\n",
    "time.sleep(3)  \n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(3)  \n",
    "\n",
    "driver.find_element(By.LINK_TEXT, \"身高\").click()\n",
    "time.sleep(3)  \n",
    "\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 隐式等待快，检测到就执行 ，≤显示等待时间\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"鹿晗\")\n",
    "driver.implicitly_wait(3)  # 隐式等待，最长等3秒\n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "driver.implicitly_wait(3)  # 隐式等待，最长等3秒\n",
    "\n",
    "driver.find_element(By.LINK_TEXT, \"身高\").click()\n",
    "time.sleep(3)  # 等待3秒\n",
    "\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### title、url"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "页面标题: 搜狗搜索引擎 - 上网从搜狗开始\n",
      "页面URL: https://www.sogou.com/\n"
     ]
    }
   ],
   "source": [
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "driver.implicitly_wait(2)  \n",
    "title = driver.title  # 获取页面标题\n",
    "print(\"页面标题:\", title)  # 打印页面标题\n",
    "\n",
    "URL = driver.current_url  # 获取当前页面URL\n",
    "print(\"页面URL:\", URL)  # 打印页面URL\n",
    "driver.quit()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 最大化、宽高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "time.sleep(2)  \n",
    "driver.maximize_window()  # 最大化浏览器窗口\n",
    "\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(1)  \n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(2)  \n",
    "driver.set_window_size(800, 600)  # 设置浏览器窗口宽高\n",
    "time.sleep(1)  \n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 滚动条"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "driver.maximize_window()  # 最大化浏览器窗口\n",
    "\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(1)  \n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(2)  \n",
    "driver.set_window_size(800, 600)  # 设置浏览器窗口宽高\n",
    "js0 = \"var q=document.documentElement.scrollTop=10000\"\n",
    "driver.execute_script(js0)  # 滚动到页面底部\n",
    "time.sleep(3)  \n",
    "\n",
    "js1 = \"var q=document.documentElement.scrollTop=0\"\n",
    "driver.execute_script(js1)  # 滚动到页面顶部\n",
    "time.sleep(2)  \n",
    "\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 前进回退"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(1)  # 等待3秒\n",
    "\n",
    "# 通过id定位搜索按钮并点击\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(2)  # 等待搜索结果加载\n",
    "\n",
    "driver.back()  # 后退\n",
    "time.sleep(3)\n",
    "driver.forward()  # 前进\n",
    "time.sleep(3)\n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 键盘事件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.common.keys import Keys "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"http://127.0.0.1:84/zentao/user-login-L3plbnRhby8=.html\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"account\").send_keys(\"admin\")\n",
    "time.sleep(2)  \n",
    "driver.find_element(By.ID, \"account\").send_keys(Keys.TAB)\n",
    "\n",
    "driver.find_element(By.NAME, \"password\").send_keys(\"yrj123456\")\n",
    "time.sleep(2)  \n",
    "driver.find_element(By.ID, \"submit\").send_keys(Keys.ENTER)\n",
    "time.sleep(2)  \n",
    "\n",
    "# 关闭浏览器\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(2)  \n",
    "\n",
    "driver.find_element(By.ID, \"query\").send_keys(Keys.CONTROL, 'a')  # 全选\n",
    "time.sleep(2)  \n",
    "driver.find_element(By.ID, \"query\").send_keys(Keys.CONTROL, 'x')  # 剪切\n",
    "time.sleep(2)  \n",
    "driver.find_element(By.ID, \"query\").send_keys(\"python\")\n",
    "time.sleep(2)\n",
    "\n",
    "driver.find_element(By.ID, \"stb\").click()\n",
    "time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "driver.quit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [],
   "source": [
    "from selenium.webdriver.common.action_chains import ActionChains"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# 指定 ChromeDriver 路径\n",
    "chrome_driver_path = \"F:/Google Chrome/chromedriver-win64/chromedriver.exe\"\n",
    "\n",
    "# 指定 Chrome 可执行文件路径\n",
    "chrome_binary_path = \"F:/Google Chrome/Google/Chrome/Application/chrome.exe\"  \n",
    "\n",
    "# 初始化浏览器配置\n",
    "options = Options()\n",
    "options.binary_location = chrome_binary_path  # 手动指定 Chrome 路径\n",
    "\n",
    "\n",
    "# 创建带路径和配置的浏览器实例\n",
    "service = Service(executable_path=chrome_driver_path)\n",
    "driver = webdriver.Chrome(service=service, options=options)\n",
    "# 打开百度首页\n",
    "driver.get(\"https://www.sogou.com/\")\n",
    "\n",
    "# 通过id定位搜索框并输入内容\n",
    "driver.find_element(By.ID, \"query\").send_keys(\"selenium\")\n",
    "time.sleep(2)  \n",
    "b= driver.find_element(By.ID, \"stb\")\n",
    "\n",
    "# b.click()\n",
    "\n",
    "ActionChains(driver).double_click(b).perform()  # 双击搜索按钮\n",
    "time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "# ActionChains(driver).context_click(b).perform()  # 右键点击搜索按钮\n",
    "# time.sleep(1)  # 等待搜索结果加载\n",
    "\n",
    "driver.quit()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "nowdelete",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
